package dnbW.dbW.sucxentdbW;

import dnbW.entityW.Cardinality;

/**
 * Encapsulates a relationship between an ID attribute and IDREF attribute
 * @author truongbaquan
 */
public class IdRelation {
    private IdPath id;

    public IdPath getId() {
        return id;
    }

    private IdRefPath idRef;

    public IdRefPath getIdRef() {
        return idRef;
    }

    /**
     * Number of ID attributes per IDREF attributes
     */
    private Cardinality idCardinality;

    public Cardinality getIdCardinality() {
        return idCardinality;
    }

    public void setIdCardinality(Cardinality cardinality) {
        this.idCardinality = cardinality;
    }

    /**
     * Number of IDREF attributes per ID attribute
     */
    private Cardinality idRefCardinality;

    public Cardinality getIdRefCardinality() {
        return idRefCardinality;
    }

    public void setIdRefCardinality(Cardinality idRefCardinality) {
        this.idRefCardinality = idRefCardinality;
    }

    /**
     * Gets the string for the type stored in the database
     * @return
     */
    public String getDbCardinality() {
        return this.idCardinality.toString() + "-" + this.idRefCardinality.toString();
    }

    public void setDbCardinality(String dbType) {
        int hyphenIndex = dbType.indexOf('-');
        if (hyphenIndex == -1) return;
        String cardinality;
        cardinality = dbType.substring(0, hyphenIndex).trim();
        if (cardinality.equals("one")) this.idCardinality = Cardinality.one;
        else if (cardinality.equals("many")) this.idCardinality = Cardinality.many;
        cardinality = dbType.substring(hyphenIndex+1).trim();
        if (cardinality.equals("one")) this.idRefCardinality = Cardinality.one;
        else if (cardinality.equals("many")) this.idRefCardinality = Cardinality.many;
    }

    public IdRelation(IdPath id, IdRefPath idRef) {
        this.id = id;
        this.idRef = idRef;
    }
}
